package com.gdssoft.pdaweb.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import com.gdssoft.core.service.impl.BaseServiceImpl;
import com.gdssoft.core.util.BeanUtil;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.pdaweb.dao.PdaMdAssetInfoDao;
import com.gdssoft.pdaweb.model.PdaMdAssetInfo;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.service.CrmService;
import com.gdssoft.pdaweb.service.PdaMdAssetInfoService;
import com.gdssoft.pdaweb.service.am.PdaAmMeterService;
import com.gdssoft.pdaweb.service.water.PdaWaterMeterService;


public class PdaMdAssetInfoServiceImpl extends BaseServiceImpl<PdaMdAssetInfo> implements PdaMdAssetInfoService {
	private PdaMdAssetInfoDao dao;
	public PdaMdAssetInfoServiceImpl(PdaMdAssetInfoDao dao) {
		super(dao);
		this.dao=dao;
	}
	@Resource
	private PdaAmMeterService pdaAmMeterService;
	@Resource
	private PdaWaterMeterService PdaWaterMeterService;
	@Resource
	private CrmService crmService;
	@Override
	public boolean saveSupply(int meterType, PdaAmMeter am, PdaMdAssetInfo asset) {
		boolean res = true;
		am.setCreateddt(new SimpleDateFormat().format(new Date()));
		am.setCreatedby(ContextUtil.getCurrentUser().getUsername());
		am.setToCrmFlag(0);
		try {
			switch (meterType) {
			case 2: // 电
				if(crmService.pushAmMeter(am)){
					am.setToCrmFlag(8);
				}else{
					am.setToCrmFlag(9);
				}
				pdaAmMeterService.save(am);
				break;
			case 1: // 水
				PdaWaterMeter pdaWaterMeter = new PdaWaterMeter();	
				BeanUtil.copyNotNullProperties(pdaWaterMeter, am);//注意：需要PdaWaterMeter与PdaAmMeter的关键字属性名一致
				if(crmService.pushWaterMeter(pdaWaterMeter)){
					pdaWaterMeter.setToCrmFlag(8);
				}else{
					pdaWaterMeter.setToCrmFlag(9);
				}
				PdaWaterMeterService.save(pdaWaterMeter);
				
				break;
			default:
				res = false;
				break;
			}
		} catch (Exception e) {
			res = false;
			e.printStackTrace();
		}
		try {
			asset = dao.getByBarCode(asset.getBarCode());
			if(asset==null){
				res = false;
			}else{
				asset.setIsSupply("Y");
				this.save(asset);	//标为已补充资产
			}
		} catch (Exception e) {
			res = false;
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public boolean isExist(int meterType, String amId) {
		boolean isExist = false;
	/*	switch (meterType) {
		case 1: // 电
			isExist = pdaAmMeterService.isExist(amId);
			break;
		case 2: // 水
			isExist = PdaWaterMeterService.isExist(amId);
			break;
		default:
			isExist = false;
			break;
		}*/
		isExist=pdaAmMeterService.isExist(amId);
		if(!isExist){
			isExist = PdaWaterMeterService.isExist(amId);
		}
		return isExist;
	}
	
	@Override
	public boolean isExist(String crmMeterNo) {
		return pdaAmMeterService.isCrmMeterNoExist(crmMeterNo)&& PdaWaterMeterService.isCrmMeterNoExist(crmMeterNo);
	}
}
